#include "fsartifactpersistenceassistant.h"

#include "../documents/swpartifactdocument.h"
#include "fspersistencecommon.h"
#include "fsprocesspersistentdocument.h"

#include <icontainerdocument.h>
#include <istorage.h>

#include <QtCore/QDataStream>
#include <QtCore/QUrl>

using namespace core;
using namespace fspersistence;
using namespace swp;

FsArtifactPersistenceAssistant::FsArtifactPersistenceAssistant(
    const MimeType& mimeType) : IPersistenceAssistant(),
        m_mimeType(mimeType),
        m_persistentDocument(),
        m_document()
{

}

FsArtifactPersistenceAssistant::~FsArtifactPersistenceAssistant()
{

}

// IPersistenceAssistant
bool FsArtifactPersistenceAssistant::canOpen(const MimeType& mimeType) const
{
    return mimeType == m_mimeType;
}

bool FsArtifactPersistenceAssistant::canSave(const MimeType& mimeType) const
{
    return mimeType == m_mimeType;
}

bool FsArtifactPersistenceAssistant::isNative(
    const QSharedPointer<IPersistentDocument>& document) const
{
    return document->mimeType() == m_mimeType;
}

bool FsArtifactPersistenceAssistant::isNative(const MimeType& mimeType) const
{
    return mimeType == m_mimeType;
}

QString FsArtifactPersistenceAssistant::forStorage() const
{
    return FILE_SYSTEM_STORAGE;
}

QSharedPointer<IDocument> FsArtifactPersistenceAssistant::open()
{
    if (!m_document.isNull())
    {

    }
    else
    {
        Q_ASSERT(!m_persistentDocument.isNull());

        QSharedPointer<IDocument> ret;

        const QSharedPointer<FsProcessPersistentDocument> _document =
            qSharedPointerDynamicCast<FsProcessPersistentDocument>(
                m_persistentDocument);

        Q_ASSERT(!_document.isNull());

        const IStorage* storage = _document->storage();
        const QByteArray data = storage->read(m_persistentDocument);
        _document->updateData(data);

        QDataStream stream(data);
        stream.setVersion(QDataStream::Qt_4_7);

        QString name;

        stream >> name;

        m_document = QSharedPointer<IDocument>(new SwpArtifactDocument(name,
            _document->mimeType(),
            QSharedPointer<IContainerDocument>(0), true));
    }

    return m_document;
}

QSharedPointer<IDocument> FsArtifactPersistenceAssistant::openFuture()
{
    Q_ASSERT(!m_persistentDocument.isNull());

    QSharedPointer<IDocument> ret;

    const QSharedPointer<FsProcessPersistentDocument> _document =
        qSharedPointerDynamicCast<FsProcessPersistentDocument>(
            m_persistentDocument);

    Q_ASSERT(!_document.isNull());

    const IStorage* storage = _document->storage();
    const QByteArray data = storage->read(m_persistentDocument);
    _document->updateData(data);

    QDataStream stream(data);
    stream.setVersion(QDataStream::Qt_4_7);

    QString name;

    stream >> name;

    m_document = QSharedPointer<IDocument>(new SwpArtifactDocument(name,
        _document->mimeType(),
        QSharedPointer<IContainerDocument>(0), true));

    return m_document;
}

QSharedPointer<IPersistentDocument> FsArtifactPersistenceAssistant::save()
{
    Q_ASSERT(!m_document.isNull());

    QByteArray data;

    QDataStream stream(&data, QIODevice::WriteOnly);
    stream.setVersion(QDataStream::Qt_4_7);

    stream << m_document->name();

    m_persistentDocument->updateData(data);
    m_persistentDocument->storage()->write(m_persistentDocument);

    return m_persistentDocument;
}

void FsArtifactPersistenceAssistant::closeAndKeepFuture()
{

}

bool FsArtifactPersistenceAssistant::createPersistentDocument(const QUrl& url,
    const IStorage* storage)
{
    bool ret = false;

    QFileInfo info(url.toLocalFile());

    if (info.exists())
    {
        QSharedPointer<IPersistentDocument> document(
            new FsProcessPersistentDocument(info, storage));

        m_persistentDocument = document;

        ret = true;
    }

    return ret;
}

QSharedPointer<IDocument> FsArtifactPersistenceAssistant::getDocument() const
{
    return m_document;
}

QSharedPointer<IPersistentDocument> FsArtifactPersistenceAssistant::
    getPersistentDocument() const
{
    return m_persistentDocument;
}

IPersistenceAssistant* FsArtifactPersistenceAssistant::copy() const
{
    FsArtifactPersistenceAssistant* ret = new FsArtifactPersistenceAssistant(
        m_mimeType);

    return ret;
}

void FsArtifactPersistenceAssistant::setDocument(
    const QSharedPointer<IDocument>& document)
{
    if (m_document.isNull())
    {
        m_document = document;
    }
    else
    {
        Q_ASSERT(false); // document is already set
    }
}

void FsArtifactPersistenceAssistant::setPersistentDocument(
    const QSharedPointer<IPersistentDocument>& document)
{
    if (m_persistentDocument.isNull())
    {
        m_persistentDocument = document;
    }
    else
    {
        Q_ASSERT(false); // document is already set
    }
}
